package com.guawazi;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.guawazi.base.BaseActivity;
import com.guawazi.base.BaseFragment;
import com.guawazi.fragment.AccelerationFragment;
import com.guawazi.fragment.MineFragment;
import com.guawazi.fragment.MonitorFragment;
import com.guawazi.fragment.TableFragment;
import com.guawazi.widget.CommonTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    CommonTitleBar titleBar;
    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar navigationBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        init();
    }

    public void setTitle(String title){
        titleBar.setTitle(title);
    }

    private void init() {
        titleBar.setLeftVisibility(View.GONE);

        //初始化底部导航栏
        navigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {//这里也可以使用SimpleOnTabSelectedListener
            @Override
            public void onTabSelected(int position) {//未选中 -> 选中
                BaseFragment fragment = null;
                switch (position){
                    case 0:
                        fragment = new MonitorFragment();
                        titleBar.setTitle("脊背弯曲度");
                        break;
                    case 1:
                        fragment = new AccelerationFragment();
                        titleBar.setTitle("加速度");
                        break;
                    case 2:
                        fragment = new TableFragment();
                        titleBar.setTitle("表格");
                        break;
                    case 3:
                        fragment = new MineFragment();
                        titleBar.setTitle("我的");
                        break;
                }
                FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction()
                        .replace(R.id.container, fragment)
                        .commit();
            }

            @Override
            public void onTabUnselected(int position) {//选中 -> 未选中

            }

            @Override
            public void onTabReselected(int position) {//选中 -> 选中

            }
        });
        navigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        navigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        navigationBar.setBarBackgroundColor(R.color.white);
        navigationBar.addItem(new BottomNavigationItem(R.drawable.foot_icon1, "监控").setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.drawable.foot_icon2, "数据").setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.drawable.foot_icon3, "表格").setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.drawable.foot_icon4, "我的").setActiveColorResource(R.color.colorPrimary))
                .setFirstSelectedPosition(0)
                .initialise(); //所有的设置需在调用该方法前完成

        //打开首页默认加载FinancingFragment
        navigationBar.selectTab(0);
    }

    //双击退出
    private long lastClickTime = 0;

    public void onBackPressed() {
        if (lastClickTime == 0) {
            Toast.makeText(this, "再次按下退出程序", Toast.LENGTH_SHORT).show();
            lastClickTime = System.currentTimeMillis();
        } else {
            long currentClickTime = System.currentTimeMillis();
            if (currentClickTime - lastClickTime < 1000) {
                finish();
            } else {
                Toast.makeText(this, "再次按下退出程序", Toast.LENGTH_SHORT).show();
                lastClickTime = currentClickTime;
            }
        }
    }

    public void setMenuClick(View.OnClickListener listener){
        titleBar.setMenuVisibility(View.VISIBLE);
        titleBar.setMenuClick(listener);
    }

    public void hideMenu(){
        titleBar.setMenuVisibility(View.GONE);
    }
}
